package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.TKeysAction;
import com.eportal.domain.Sysopt;
import com.eportal.params.background.TKeys.TKeyParam;
import com.eportal.service.TKeysService;
import com.eportal.vo.controller.background.tkeys.TKeyListVO;
import com.framework.jqgrid.Page;
import com.framework.jqgrid.PageEntity;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 钥匙管理
 * Created by Saber on 2017/8/14.
 */
@Controller
@RequestMapping(value = "/admin/tkeys")
public class TKeyController {

    private static Logger logger = LoggerFactory.getLogger(TKeyController.class.getName());

    @Resource
    private TKeysService tKeysService;

    /**
     * 钥匙管理列表接口
     * @author Saber
     * @Date 2017/8/14 上午11:15
     * @param request
     * @param pageEntity    分页对象
     * @param param         查询参数
     * @see TKeysAction#page()
     * @return
     */
    @RequestMapping(value = "page.json")
    @ResponseBody
    public JSONObject page(HttpServletRequest request,PageEntity pageEntity,TKeyParam param){

        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            PageInfo<TKeyListVO> pageInfo=tKeysService.queryForPage(pageEntity,param,user,userRole);

            json.put("total",pageInfo.getTotal());

            if(pageInfo.getList().size()==0){
                json.put("msg","暂无数据");
            }else{
                json.put("msg","ok");
            }
            json.put("data",pageInfo.getList());
            Page page = new Page(pageInfo,pageEntity.getPageNo());
            json.put("page", page);
            json.put("code",1);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("门禁管理列表异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 修改有效期或状态
     * 包含修改钥匙有效期和修改钥匙状态
     * @author Saber
     * @Date 2017/8/14 下午5:49
     * @param request
     * @param sIds       sId集合 使用,分割
     * @param status    状态 默认,0正常，1停用,2:设备被回收（永久禁用）
     * @param validity  时间
     * @see TKeysAction#changeKeys()
     * @see TKeysAction#changeKeysValidity()
     * @return
     */
    @RequestMapping(value = "changeKeys.json",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject changeKeys(HttpServletRequest request,String sIds,String status,String validity){
        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {

            String[] sids = sIds.split(",");
            for(String id:sids){
                json=tKeysService.changeKeys(Long.valueOf(id), status, validity,user,userRole);
            }
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("智能门禁更换详情异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }
}
